﻿namespace BTS.COMMON
{
    using System;
    using System.IO;
    using System.Text;
    using System.Web;
    using System.Web.UI.WebControls;

    public abstract class FileHelper
    {
        protected FileHelper()
        {
        }

        public static void CheckDirectory(string path)
        {
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
        }

        public static bool CheckFile(string path)
        {
            FileInfo info = new FileInfo(path);
            return info.Exists;
        }

        public static bool CopyDirectory(string srcPath, string aimPath)
        {
            try
            {
                if (Directory.Exists(srcPath))
                {
                    if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar)
                    {
                        aimPath = aimPath + Path.DirectorySeparatorChar;
                    }
                    if (!Directory.Exists(aimPath))
                    {
                        Directory.CreateDirectory(aimPath);
                    }
                    string[] fileSystemEntries = Directory.GetFileSystemEntries(srcPath);
                    foreach (string str in fileSystemEntries)
                    {
                        if (Directory.Exists(str))
                        {
                            CopyDirectory(str, aimPath + Path.GetFileName(str));
                        }
                        else
                        {
                            File.Copy(str, aimPath + Path.GetFileName(str), true);
                        }
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool CopyFile(string srcPath, string aimPath, string fileName)
        {
            try
            {
                if (File.Exists(srcPath))
                {
                    CheckDirectory(aimPath);
                    if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar)
                    {
                        aimPath = aimPath + Path.DirectorySeparatorChar;
                    }
                    File.Copy(srcPath, aimPath + fileName, true);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static void DeleteDirectory(string dirPath)
        {
            DirectoryInfo info = new DirectoryInfo(dirPath);
            if (info.Exists)
            {
                info.Delete(true);
            }
        }

        public static void DeleteFile(string filePath)
        {
            FileInfo info = new FileInfo(filePath);
            if (info.Exists)
            {
                info.Delete();
            }
        }

        public static string FileReader(string path)
        {
            if (!File.Exists(path))
            {
                return string.Empty;
            }
            StreamReader reader = new StreamReader(path, Encoding.GetEncoding("gb2312"));
            string str = reader.ReadToEnd();
            reader.Close();
            return str;
        }

        public static bool FileRename(string path, string oldName, string newName)
        {
            if (!Directory.Exists(path))
            {
                return false;
            }
            if (path[path.Length - 1] != Path.DirectorySeparatorChar)
            {
                path = path + Path.DirectorySeparatorChar;
            }
            if (!File.Exists(path + oldName))
            {
                return false;
            }
            if (File.Exists(path + newName))
            {
                return false;
            }
            File.Move(path + oldName, path + newName);
            return true;
        }

        public static void FileWriter(string path, string content)
        {
            string str = path.Remove(path.LastIndexOf(@"\"));
            if (!Directory.Exists(str))
            {
                Directory.CreateDirectory(str);
            }
            StreamWriter writer = new StreamWriter(path, false, Encoding.GetEncoding("gb2312"));
            writer.WriteLine(content);
            writer.Flush();
            writer.Close();
        }

        private static double GetFilesSize(string path)
        {
            DirectoryInfo info = new DirectoryInfo(path);
            double num = 0.0;
            foreach (FileSystemInfo info2 in info.GetFileSystemInfos())
            {
                if (info2 is FileInfo)
                {
                    num += ((FileInfo) info2).Length;
                }
                else
                {
                    num += GetFilesSize(info2.FullName);
                }
            }
            return num;
        }

        public static double GetFilesSize(string path, string unit)
        {
            double filesSize = GetFilesSize(path);
            switch (unit.ToLower())
            {
                case "byte":
                    return filesSize;

                case "kb":
                    return (filesSize / 1024.0);

                case "mb":
                    return ((filesSize / 1024.0) / 1024.0);

                case "gb":
                    return (((filesSize / 1024.0) / 1024.0) / 1024.0);
            }
            return 0.0;
        }

        public static string Upload(FileUpload file, string filename, string savePath, string ext, int maxSize, string delFilename)
        {
            if (!file.HasFile)
            {
                return null;
            }
            return Upload(file.PostedFile, filename, savePath, ext, maxSize, delFilename);
        }

        public static string Upload(HttpPostedFile file, string filename, string savePath, string ext, int maxSize, string delFilename)
        {
            if (file.ContentLength < 0 || string.IsNullOrEmpty(file.FileName))
            {
                return null;
            }
            string[] strArray = ext.Split(new char[] { '|' });
            string str = string.Empty;
            string[][] strArray2 = new string[][] { new string[] { "bmp", "image/bmp" }, new string[] { "png", "image/x-png" }, new string[] { "gif", "image/gif" }, new string[] { "jpg", "image/pjpeg" }, new string[] { "jpeg", "image/pjpeg" }, new string[] { "txt", "text/plain" }, new string[] { "doc", "application/msword" }, new string[] { "xls", "application/vnd.ms-excel" }, new string[] { "ppt", "application/vnd.ms-powerpoint" }, new string[] { "pdf", "application/pdf" }, new string[] { "rar", "application/octet-stream" }, new string[] { "mp3", "audio/mpeg" }, new string[] { "wma", "audio/x-ms-wma" } };
            bool flag = false;
            foreach (string str2 in strArray)
            {
                if (file.FileName.ToLower().EndsWith("." + str2))
                {
                    str = str2;
                    flag = true;
                    break;
                }
            }
            if (!flag)
            {
                return null;
            }
            flag = false;
            foreach (string[] strArray3 in strArray2)
            {
                if (str.Equals(strArray3[0]) && file.ContentType.Equals(strArray3[1]))
                {
                    flag = true;
                    break;
                }
            }
            if (!flag)
            {
                return null;
            }
            if (file.ContentLength > (maxSize * 1024))
            {
                return null;
            }
            if (!string.IsNullOrEmpty(delFilename))
            {
                DeleteFile(savePath + delFilename);
            }
            filename = filename + "." + str;

            DirectoryInfo upDir = new DirectoryInfo(savePath);
            if (!upDir.Exists)
            {
                upDir.Create();
            }

            file.SaveAs(savePath + filename);
            return filename;
        }

        public static string AppHttpRoot
        {
            get
            {
                if (HttpContext.Current.Request.ApplicationPath.Equals("/"))
                {
                    return ("http://" + HttpContext.Current.Request.Url.Host + "/");
                }
                return ("http://" + HttpContext.Current.Request.Url.Host + HttpContext.Current.Request.ApplicationPath + "/");
            }
        }

        public static string AppPhyRoot
        {
            get
            {
                return HttpContext.Current.Request.PhysicalApplicationPath;
            }
        }

        public static string AppVirRoot
        {
            get
            {
                if (HttpContext.Current.Request.ApplicationPath.Equals("/"))
                {
                    return "/";
                }
                return (HttpContext.Current.Request.ApplicationPath + "/");
            }
        }
    }
}

